package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.CouponsDto;
import com.dingreading.cloud.admin.entity.Coupons;
import com.dingreading.cloud.admin.entity.table.CouponsTableDef;
import com.dingreading.cloud.admin.mapper.CouponsMapper;
import com.dingreading.cloud.admin.service.CouponsService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CouponsServiceImpl extends BaseServiceImpl<CouponsMapper, Coupons, CouponsTableDef> implements CouponsService {

    @Override
    protected CouponsTableDef getTable() {
        return CouponsTableDef.coupons;
    }

    @Override
    public Page<CouponsDto> pageList(PageUtil pageUtil, Long typeId, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (typeId != null)
            condition.and(table.typeId.eq(typeId));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.couponName.like(keyword));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.enabled.desc(), table.id.desc());

        return getMapper().paginateWithRelationsAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, CouponsDto.class);
    }

    @Override
    public boolean updateEnabled(Long id, Integer enabled) {
        return UpdateChain.of(Coupons.class)
                .set(table.enabled, enabled)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public int dropData(Long id) {
        return getMapper().deleteByCondition(table.id.eq(id));
    }

    @Override
    public CouponsDto dtoById(Long id) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.id.eq(id));
        return getMapper().selectOneWithRelationsByQueryAs(wrapper, CouponsDto.class);
    }

    @Override
    public boolean updateInfo(
            Long id, Long typeId, String couponName, String intro, String goodsIds, String goodsCategoryIds,
            String staffUid, String staffName
    ) {
        UpdateChain<Coupons> updateChain = UpdateChain.of(Coupons.class);
        if (couponName != null)
            updateChain.set(table.couponName, couponName);
        updateChain.set(table.intro, StringUtils.defaultString(intro));
        if (typeId == 3) {
            if (StringUtils.isNotBlank(goodsIds)) {
                updateChain.set(table.goodsIds, StringUtils.defaultString(goodsIds));
            }
            if (StringUtils.isNotBlank(goodsCategoryIds)) {
                updateChain.set(table.goodsCategoryIds, StringUtils.defaultString(goodsCategoryIds));
            }
        }

        updateChain.set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName));

        return updateChain
                .where(table.id.eq(id))
                .update();
    }

}
